/**
 * 详情对话框
 */
var BasicPositionInfoDlg = {
    data: {
        positionAreaId: "",
        positionAreaName: "",
        positionName: "",
        positionNumber: "",
        positionType: "",
        positionFunction: "",
        positionCondition: "",
        positionAreaType: "",
        positionFloor: "",
        positionLine: "",
        positionColumn: "",
        positionLayer: "",
        positionLength: "",
        positionWidth: "",
        positionHeight: "",
        positionWeight: "",
        positionBarcode: "",
        positionProduct: "",
        positionMeasure: "",
        positionProducttype: "",
        positionPutorder: "",
        positionPickingorder: "",
        positionAvailable: "",
        positionAlready: "",
        positionState: "",
        positionNotes: "",
        version: "",
        deliverId: "",
        createTime: "",
        updateTime: "",
        createUser: "",
        updateUser: "",
        positionXlbId:""
    }
};

var HxBasicGoodsInfoDlg = {
    data: {
		id:"",
        goodsCustomerId: "",
        goodsCustomerName: "",
        goodsName: "",
        goodsNumber: "",
        goodsPcode69: "",
        goodsXcode69: "",
        goodsHelpcode: "",
        goodsSpecs: "",
        goodsBigtype: "",
        goodsBrand: "",
        jibenUnit: "",
        kcUnit: "",
        kcRate: "",
        cgUnit: "",
        cgRate: "",
        pfUnit: "",
        pfRate: "",
        psUnit: "",
        psRate: "",
        anquanNum: "",
        goodsBottom: "",
        goodsLamination: "",
        goodsTraylayer: "",
        goodsSupportvolume: "",
        length: "",
        height: "",
        width: "",
        goodsWeight: "",
        goodsVolume: "",
        goodsChargingCg: "",
        goodsChargingPs: "",
        goodsChargingPf: "",
        goodsWarranty: ""
    }
};

layui.use(['table', 'form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/basicPosition/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('basicPositionForm', result.data);

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/basicPosition/editItem", function (data) {
            Feng.success("更新成功！");
        	var Indexs = parent.layer.getFrameIndex(window.name);
            parent.layer.close(Indexs);
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Indexs = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Indexs);
    });
    
    //查询库区
    form.on('select(warehouseId)',function(){
    	$("#positionAreaId").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/basicArea/queryArea',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"areaWarehouseId":$("#warehouseId").val()},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>请选择</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.id+"'>"+value.areaName+","+value.areaNumber+"</option>";
    	        	});
    	        	$("#positionAreaId").html(str);
    	        }
    	    }
    	});
        form.render('select');
    });
    
    //点击商品名称
    $("#selectgoods").click(function(){
        layer.open({
            type: 2,
            title: '选择商品',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/basicPosition/toChoiceGoods',
            end: function () {
                $("#positionProducttype").val(HxBasicGoodsInfoDlg.data.goodsNumber);//商品编号
            }
        });
    });

    /**
     * 库位商品关联表管理
     */
    var HxBasicPositionGoods = {
        tableId: "hxBasicPositionGoodsTable"
    };

    /**
     * 初始化表格的列
     */
    HxBasicPositionGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'positionNumber', sort: true, title: '库位编号'},
            {field: 'goodsId', hide: true, title: '商品id'},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'anquanNum', sort: true, title: '<span style="color: #2b85e4;"><i class="layui-icon">&#xe716;</i> 安全库存值</span>', edit:'text', style:'color: #2b85e4;'},
            {field: 'psUnit', sort: true, title: '配送单位'},
            {field: 'psRate', sort: true, title: '配送换算率'},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'operator', sort: true, title: '操作人'}


        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxBasicPositionGoods.tableId,
        url: Feng.ctxPath + '/hxBasicPositionGoods/list?positionId='+Feng.getUrlParam("id"),
        page: true,
        limit: 40,
        height: 350,
        cellMinWidth: 100,
        cols: HxBasicPositionGoods.initColumn()
    });

    // 编辑单元格 事件
    table.on('edit(' + HxBasicPositionGoods.tableId + ')', function (obj) {
        var data = obj.data;//得到所在行所有键值
		var value = obj.value;//修改后的值
        var field = obj.field;
		if(field == 'anquanNum'){
	        var ajax = new $ax(Feng.ctxPath + "/hxBasicPositionGoods/editItem?id="+data.id+"&anquanNum="+value+"&goodsId="+data.goodsId, function (data) {
	    		if (data.success) {
	                Feng.success("修改成功!");
				} else {
	                Feng.error("修改失败！"+data.message);
				}
	        }, function (data) {
	            Feng.error("修改失败!" + data.responseJSON.message + "!");
	        });
	        ajax.start();
		}
    });

    $('#btnDelete').click(function () {
    	var checkStatus = table.checkStatus('hxBasicPositionGoodsTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/hxBasicPositionGoods/btnDelete?ids="+ids, function (data) {
	                if(data.success){
	                	Feng.success("删除成功!");
	                	table.reload(HxBasicPositionGoods.tableId);
	                } else {
	                    Feng.error("删除失败!" + data.message + "!");
	                }
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除选中明细?", operation);
		} else {
			layer.alert('请至少选中一条数据');
		}
    });
    
    $("#btnAdd").click(function(){
        layer.open({
            type: 2,
            title: '选择商品',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/basicPosition/toChoiceGoods?positionId='+ $("#positionId").val(),
            end: function () {
            	table.reload(HxBasicPositionGoods.tableId);
            }
        });
    });

});